home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / Utilities / C / Mesa / samples / blendxor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-29  |  3.5 KB  |  174 lines

  1. /*
  2. ** blendxor.c - Demonstrates the use of the blend_logic_op
  3. **    extension to draw hilights.  Using XOR to draw the same
  4. **    image twice restores the background to its original value.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include "gltk.h"
  12.  
  13.  
  14. GLenum doubleBuffer, directRender;
  15. int dithering = 0;
  16. GLint windW, windH;
  17.  
  18. static void Init(void)
  19. {
  20.     glDisable(GL_DITHER);
  21.     glShadeModel(GL_FLAT);
  22. }
  23.  
  24. static void Reshape(int width, int height)
  25. {
  26.  
  27.     windW = (GLint)width;
  28.     windH = (GLint)height;
  29.  
  30.     glViewport(0, 0, (GLint)width, (GLint)height);
  31.  
  32.     glMatrixMode(GL_PROJECTION);
  33.     glLoadIdentity();
  34.     gluOrtho2D(0, 400, 0, 400);
  35.     glMatrixMode(GL_MODELVIEW);
  36. }
  37.  
  38. static GLenum Key(int key, GLenum mask)
  39. {
  40.  
  41.     switch (key) {
  42.       case TK_ESCAPE:
  43.     tkQuit();
  44.       case TK_d:
  45.     dithering = !dithering;
  46.     break;
  47.       default:
  48.     return GL_FALSE;
  49.     }
  50.     return GL_TRUE;
  51. }
  52.  
  53. static void Draw(void)
  54. {
  55.     int i;
  56.  
  57.     glDisable(GL_BLEND);
  58.  
  59.     (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  60.  
  61.     glClearColor(0.5, 0.6, 0.1, 1.0);
  62.     glClear(GL_COLOR_BUFFER_BIT);
  63.  
  64.     /* Draw background prims */
  65.     glColor3f(0.1, 0.1, 1.0);
  66.     glBegin(GL_TRIANGLES);
  67.         glVertex2i(5, 5);
  68.         glVertex2i(130, 50);
  69.         glVertex2i(100,  300);
  70.     glEnd();
  71.     glColor3f(0.5, 0.2, 0.9);
  72.     glBegin(GL_TRIANGLES);
  73.         glVertex2i(200, 100);
  74.         glVertex2i(330, 50);
  75.         glVertex2i(340,  400);
  76.     glEnd();
  77.  
  78.     glEnable(GL_BLEND);
  79.     glBlendEquationEXT(GL_LOGIC_OP);
  80.     glLogicOp(GL_XOR);
  81.  
  82.     /* Draw a set of rectangles across the window */
  83.     glColor3f(0.9, 0.2, 0.8);
  84.     for(i = 0; i < 400; i+=60) {
  85.         glBegin(GL_POLYGON);
  86.             glVertex2i(i, 100);
  87.             glVertex2i(i+50, 100);
  88.             glVertex2i(i+50, 200);
  89.             glVertex2i(i, 200);
  90.         glEnd();
  91.     }
  92.     glFlush();   /* Added by Brian Paul */
  93.     sleep(2);
  94.  
  95.     /* Redraw  the rectangles, which should erase them */
  96.     for(i = 0; i < 400; i+=60) {
  97.         glBegin(GL_POLYGON);
  98.             glVertex2i(i, 100);
  99.             glVertex2i(i+50, 100);
  100.             glVertex2i(i+50, 200);
  101.             glVertex2i(i, 200);
  102.         glEnd();
  103.     }
  104.     glFlush();
  105.  
  106.  
  107.     if (doubleBuffer) {
  108.     tkSwapBuffers();
  109.     }
  110. }
  111.  
  112. static GLenum Args(int argc, char **argv)
  113. {
  114.     GLint i;
  115.  
  116.     doubleBuffer = GL_FALSE;
  117.     directRender = GL_TRUE;
  118.  
  119.     for (i = 1; i < argc; i++) {
  120.     if (strcmp(argv[i], "-sb") == 0) {
  121.         doubleBuffer = GL_FALSE;
  122.     } else if (strcmp(argv[i], "-db") == 0) {
  123.         doubleBuffer = GL_TRUE;
  124.     } else if (strcmp(argv[i], "-dr") == 0) {
  125.         directRender = GL_TRUE;
  126.     } else if (strcmp(argv[i], "-ir") == 0) {
  127.         directRender = GL_FALSE;
  128.     } else {
  129.         printf("%s (Bad option).\n", argv[i]);
  130.         return GL_FALSE;
  131.     }
  132.     }
  133.     return GL_TRUE;
  134. }
  135.  
  136. void main(int argc, char **argv)
  137. {
  138.     GLenum type;
  139.     char *s;
  140.     char *extName = "GL_EXT_blend_logic_op";
  141.  
  142.     if (Args(argc, argv) == GL_FALSE) {
  143.     tkQuit();
  144.     }
  145.  
  146.     tkInitPosition(0, 0, 400, 400);
  147.  
  148.     type = TK_RGB;
  149.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  150.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  151.     tkInitDisplayMode(type);
  152.  
  153.     if (tkInitWindow("Blend XOR") == GL_FALSE) {
  154.     tkQuit();
  155.     }
  156.  
  157.     /* Make sure blend_logic_op extension is there. */
  158.     s = (char *) glGetString(GL_EXTENSIONS);
  159.     if (!s)
  160.     tkQuit();
  161.     if (strstr(s,extName) == 0) {
  162.     printf("Blend_logic_op extension is not present.\n");
  163.     tkQuit();
  164.     }
  165.  
  166.     Init();
  167.  
  168.     tkExposeFunc(Reshape);
  169.     tkReshapeFunc(Reshape);
  170.     tkKeyDownFunc(Key);
  171.     tkDisplayFunc(Draw);
  172.     tkExec();
  173. }
  174.